package com.aluvfy.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

// IDEA 开发测试
public class JDBCTest01 {
    public static void main(String[] args) {
        //读取属性配置文件，读取数据库连接
        ResourceBundle bundle = ResourceBundle.getBundle("com.aluvfy.jdbc.jdbc");
        String driver = bundle.getString("driver");
        String url = bundle.getString("url");
        String user = bundle.getString("user");
        String password = bundle.getString("password");

        Connection conn = null;
        try {
            //1. 注册驱动
//            Class.forName("com.mysql.cj.jdbc.Driver");
            Class.forName(driver);
            //2. 获取连接
//            String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&serverTimezone=Asia/Shanghai&useSSL=true&characterEncoding=utf-8";
//            String user = "root";
//            String password = "root";
            conn = DriverManager.getConnection(url, user, password);
            System.out.println(conn);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
